/*
 * igo: com.cctv.igo.admin.scheduler.ui.TriggerTaskAction.java Create On 2011-5-5 上午09:35:05 By Q-Wang
 * $Revision: 1.2 $
 */
package com.ctvit.scheduler.ui;

import java.util.List;

import com.ctvit.scheduler.dto.ScheduledTask;
import com.ctvit.scheduler.dto.Task;
import com.ctvit.scheduler.dto.Trigger;
import com.ctvit.scheduler.service.ScheduleService;
import com.ctvit.scheduler.service.TaskService;
import com.ctvit.scheduler.service.TriggerService;
import com.ctvit.framework.struts2.ActionSupport;
import com.ctvit.framework.web.context.ContextHolder;

/**
 * @author <a href="mailto:apeidou@gmail.com">Q-Wang</a>
 *
 */
public class TriggerTaskAction extends ActionSupport {

	private TriggerService triggerService = ContextHolder.getBean(TriggerService.class);
	private ScheduleService scheduleService = ContextHolder.getBean(ScheduleService.class);
	private TaskService taskService = ContextHolder.getBean(TaskService.class);

	private Trigger trigger;

	private List<ScheduledTask> scheduledTasks;
	private ScheduledTask taskDefinition;
	private Task task;

	/**
	 * @return the trigger
	 */
	public Trigger getTrigger() {
		return trigger;
	}

	/**
	 * @param trigger the trigger to set
	 */
	public void setTrigger(Trigger trigger) {
		this.trigger = trigger;
	}

	/**
	 * @return the scheduledTasks
	 */
	public List<ScheduledTask> getScheduledTasks() {
		return scheduledTasks;
	}

	/**
	 * @param scheduledTasks the scheduledTasks to set
	 */
	public void setScheduledTasks(List<ScheduledTask> scheduledTasks) {
		this.scheduledTasks = scheduledTasks;
	}

	/**
	 * @return the taskDefinition
	 */
	public ScheduledTask getTaskDefinition() {
		return taskDefinition;
	}

	/**
	 * @param taskDefinition the taskDefinition to set
	 */
	public void setTaskDefinition(ScheduledTask taskDefinition) {
		this.taskDefinition = taskDefinition;
	}

	/**
	 * @return the task
	 */
	public Task getTask() {
		return task;
	}

	/**
	 * @param task the task to set
	 */
	public void setTask(Task task) {
		this.task = task;
	}


	public String list() {
		trigger = triggerService.get(trigger.getId());
		scheduledTasks = scheduleService.listScheduledTaskDetails(trigger.getId());
		return LIST;
	}

	/**
	 * @return
	 */
	public String updateTask() {
		taskDefinition.setTaskId(task.getId());
		taskDefinition.setTriggerId(trigger.getId());
		scheduleService.updateScheduledTask(taskDefinition);
		addActionMessage("任务定义更新成功: task.id=" + task.getId() + " trigger.id=" + trigger.getId());
		taskDefinition = scheduleService.getScheduledTask(trigger.getId(), task.getId());
		task = taskService.get(task.getId());
		trigger = triggerService.get(trigger.getId());
		return list();
	}

	public String addTask() {
		scheduleService.addTaskToTrigger(task.getId(), trigger.getId());
		addActionMessage("任务-触发器 关联成功: task.id=" + task.getId());
		return list();
	}

	public String removeTask() {
		scheduleService.removeTaskFromTrigger(task.getId(), trigger.getId());
		addActionMessage("任务-触发器 解除关联成功: task.id=" + task.getId());
		return list();
	}

	public String editTask() {
		taskDefinition = scheduleService.getScheduledTask(trigger.getId(), task.getId());
		task = taskService.get(task.getId());
		trigger = triggerService.get(trigger.getId());
		return EDIT;
	}
}
